package com.cug.zfanspring.config;

import com.cug.zfanspring.bean.Device;
import com.cug.zfanspring.bean.Factor;
import com.cug.zfanspring.service.DeviceDataService;
import com.cug.zfanspring.service.FactorDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.sql.Date;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.List;

@Configuration //1.主要用于标记配置类，兼备Component的效果。

@EnableScheduling // 2.开启定时任务

public class SimpleScheduleConfig {

    //3.添加定时任务
    @Autowired
    DeviceDataService deviceDataService;
    @Autowired
    FactorDataService factorDataService;
    @Scheduled(cron = "0 0/1 * * * ?")

    private void configureTasks() {


        System.err.println("执行定时任务1: " + LocalDateTime.now());

     List<Device> deviceData= deviceDataService.getLatestAlarmDeviceData();
        List<Factor> factorData= factorDataService.getLatestAlarmFactorData();

        long time=1*24*3600*1000;
            for(Device e:deviceData){
            Timestamp timestamp=e.getTime();
            timestamp.setTime(timestamp.getTime()+time);
            e.setTime(timestamp);
        }
        for(Factor e:factorData){
            Timestamp timestamp=e.getTime();
            timestamp.setTime(timestamp.getTime()+time);
            e.setTime(timestamp);
        }
        deviceDataService.updateLatestDeviceData(deviceData);
        deviceDataService.updateDeviceData(deviceData);
        factorDataService.updateLatestFactorData(factorData);
        factorDataService.updateFactorData(factorData);

    }

}